package com.cxcy.swustlearner.filter;
import com.alibaba.fastjson.JSON;
import com.cxcy.swustlearner.pojo.StaticUser;
import com.cxcy.swustlearner.pojo.User;
import com.cxcy.swustlearner.utils.JwtUtils;
import common.Result;
import jakarta.servlet.*;
import jakarta.servlet.Filter;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.IOException;
@Service
@WebFilter(urlPatterns = "/*")
public class LoginCheck implements Filter {
    @Autowired
    private JwtUtils jwtUtils;
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;//强转
        HttpServletResponse response = (HttpServletResponse) servletResponse;//强转
        String url = request.getRequestURI();
        System.out.println(url);
        if (url.contains("login")||url.contains("register")) {
            filterChain.doFilter(request, response);
            return;
        }
        String jwt=request.getHeader("token");
        System.out.println("jwt"+jwt);
        if (!StringUtils.hasLength(jwt)) {
            System.out.println(jwt);
            System.out.println("请求头为空");
            Result result = new Result();
            result.setMsg("Not_Login");
            String json = JSON.toJSONString(result);//利用阿里巴巴提供的工具类进行转换
            response.getWriter().write(json);//把字符串响应给浏览器
        }else {
            try {
                JwtUtils.parseJWT(jwt);
            } catch (Exception e) {
                Result result = new Result();
                result.fail("令牌解析失败");
                String json = JSON.toJSONString(result);
                response.getWriter().write(JSON.toJSONString(result));
                throw new RuntimeException(e);
            }
            System.out.println(JwtUtils.parseJWT(jwt).get("username"));
            StaticUser.username= (String) JwtUtils.parseJWT(jwt).get("username");
            StaticUser.permission=(int)JwtUtils.parseJWT(jwt).get("permission");
            StaticUser.classId=(String) JwtUtils.parseJWT(jwt).get("classId");
            StaticUser.collegeId=(String) JwtUtils.parseJWT(jwt).get("collegeId");
            StaticUser.majorId=(String) JwtUtils.parseJWT(jwt).get("majorId");
            StaticUser.studentId=(String) JwtUtils.parseJWT(jwt).get("studentId");
            StaticUser.uid=(String) JwtUtils.parseJWT(jwt).get("uid");
            System.out.println("filter"+JwtUtils.parseJWT(jwt).get("studentId"));
//            String json = JSON.toJSONString(JwtUtils.parseJWT(jwt));
//            System.out.println(json);
            filterChain.doFilter(request, response);
        }
    }
}